from django.db import models

# Create your models here.
from django.db import models
from django.utils import timezone
from django.conf import settings


class VerificationCode(models.Model):
    """
    验证码模型（用于数据库存储验证码）
    """
    phone = models.CharField(max_length=11, verbose_name='手机号')
    code = models.CharField(max_length=6, verbose_name='验证码')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    is_used = models.BooleanField(default=False, verbose_name='是否已使用')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name='用户')

    class Meta:
        verbose_name = '验证码'
        verbose_name_plural = '验证码'
        ordering = ['-created_at']

    def is_expired(self):
        """
        检查验证码是否过期（5分钟）
        """
        from datetime import timedelta
        return self.created_at + timedelta(minutes=5) < timezone.now()